/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "pid.h"
#include "ultrasonic.h"
#include "string.h"
#include "Routing.h"
#include "rc_cmd.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */



/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/*Esp变量*/
uint8_t Esp_recvBuff;
uint8_t Esp_buffer;
uint8_t Esp_buff;
volatile uint8_t Esp_recvLength = 0;  
volatile uint8_t Esp_recvDndFlag = 0; 
volatile uint8_t Esp_Flag = 0;

//Arm PV
uint8_t Arm_recvBuff;
uint8_t Arm_buffer;
uint8_t Arm_buff;
volatile uint8_t Arm_recvLength = 0;  
volatile uint8_t Arm_recvDndFlag = 0; 
volatile uint8_t Arm_Flag = 0;
uint8_t PV11 = 0x11; 
uint8_t PV12 = 0x12;
uint8_t PV21 = 0x21;
uint8_t PV31 = 0x31;

/*树莓派变量*/
uint8_t Pi_buffer;

volatile uint8_t Pi_flag = 0;
volatile uint8_t UART1_RECEIVE_COMPLETE = FALSE;
volatile uint8_t UART2_RECEIVE_COMPLETE = FALSE;
uint8_t state;

/*设置遥控变量*/
uint8_t BlueTooth_Value;
uint8_t type;

/*rc_cmd*/
uint8_t RC_CODE;

int a = 0;

uint8_t recvBuff[BUFFER_SIZE];
volatile uint8_t recvLength = 0;  
volatile uint8_t recvDndFlag = 0; 

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/*左右转向*/
void Trun_Left()
{
	final_pid_set(0,50);
	HAL_Delay(1000);
}

void Trun_Right()
{
	final_pid_set(50,0);
	HAL_Delay(1000);
}

void spin()
{
	final_pid_set(60,60);
	HAL_Delay(2000);
}


/*小车寻迹逻辑*///////////////////////////////////////////////////////////////
void Find_Line()
{
	int Read_RR = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15);
	int Read_R 	= HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14);
	int Read_L  = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13);
	int Read_LL = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12);
	
//	Avoid_Barrier();
	//1 直行    1 1 1 1
	if     (Read_LL == 1 && Read_L == 1 && Read_R == 1 && Read_RR == 1)
	{
		final_pid_set(50,50);
	}
	//2 左微转  1 0 1 1
	else if(Read_LL == 1 && Read_L == 0 && Read_R == 1 && Read_RR == 1)
	{
		final_pid_set(0,40);
	}
	//3 右微转  1 1 0 1
	else if(Read_LL == 1 && Read_L == 1 && Read_R == 0 && Read_RR == 1)
	{
		final_pid_set(40,0);
	}
	//4 左中转  0 0 1 1
	else if(Read_LL == 0 && Read_L == 0 && Read_R == 1 && Read_RR == 1)
	{
		final_pid_set(0,45);
	}
	//5 右中转  1 1 0 0
	else if(Read_LL == 1 && Read_L == 1 && Read_R == 0 && Read_RR == 0)
	{
		final_pid_set(45,0);
	}
	//6 左大转  0 1 1 1
	else if(Read_LL == 0 && Read_L == 1 && Read_R == 1 && Read_RR == 1)
	{
		final_pid_set(0,70);
	}
	//7 右大转  1 1 1 0
	else if(Read_LL == 1 && Read_L == 1 && Read_R == 1 && Read_RR == 0)
	{
		final_pid_set(70,0);
	}
	//8 停止    0 0 0 0
	else if(Read_LL == 1 && Read_L == 1 && Read_R == 1 && Read_RR == 1)
	{
		final_pid_set(0,0);
	}
}

/*树莓派通讯部分*////////////////////////////
/*开灯*/
void Open_The_Light()
{
	HAL_GPIO_WritePin(GPIOF,GPIO_PIN_11,GPIO_PIN_SET);
}

/*关灯*/
void Close_The_Light()
{
	HAL_GPIO_WritePin(GPIOF,GPIO_PIN_11,GPIO_PIN_RESET);
}

/*发送消息给树莓派*/
void SEND_MESSAGE_PI()
{
	if(a == 0)
	{
		HAL_UART_Transmit_DMA(&huart4,(uint8_t *)"start_face_recognition",sizeof("start_face_recognition")-1);
		a++;
	}
}

void Final_PI()
{
	SEND_MESSAGE_PI();
	while(Pi_buffer != 1);
	
}

void USART_1_IT()
{
		if(UART1_RECEIVE_COMPLETE == TRUE)
		{
			UART1_RECEIVE_COMPLETE = FALSE;
			HAL_UART_Receive_IT(&huart1,&Esp_recvBuff,1);
		}
}

void USART_2_IT()
{
		if(UART2_RECEIVE_COMPLETE == TRUE)
		{
			UART2_RECEIVE_COMPLETE = FALSE;
			HAL_UART_Receive_IT(&huart2,&Arm_recvBuff,1);
		}
}

void Transmit_1()
{		
		if(Esp_Flag == 0)
		{
			HAL_UART_Transmit(&huart1,(uint8_t *)"A",1,0xffff);
			HAL_Delay(1000);
		}
		if(Esp_buff == 'E')//the esp has got the message,start waiting
		{
			Esp_Flag = 1;//stop transmit 'A'
		}
		if(Esp_buff == 'B')
		{
			if(Arm_Flag == 0)//get the medince
			{
				HAL_UART_Transmit(&huart2,&PV11,1,0xffff);
				HAL_Delay(1000);
			}
			
			if(Arm_buff == PV21)//successfully get the medince
			{
				Arm_Flag = 3;
				Esp_Flag = 2;
			}
			
//			if(Arm_Flag == 4)//give the medince
//			{
//				HAL_UART_Transmit(&huart2,&PV21,1,0xffff);
//				HAL_Delay(1000);
//			}
//			
//			if(Arm_buff == PV31)//successfully give the medince
//			{
//				Arm_Flag = 3;
//				Esp_Flag = 2;
//			}
		
			//the door has already opened
		}
		if(Esp_Flag == 2)
		{
			HAL_UART_Transmit(&huart1,(uint8_t *)"C",1,0xffff);
			HAL_Delay(1000);
		}
		if(Esp_buff == 'F')
		{
			Esp_Flag = 3;//stop transmit 'C'
		}
		if(Esp_buff == 'D')
		{
			//the door has already closed
		}
		USART_1_IT();
		USART_2_IT();
}

void Transmit_2()
{		
		if(Esp_Flag == 3)
		{
			HAL_UART_Transmit(&huart1,(uint8_t *)"A",1,0xffff);
			HAL_Delay(1000);
		}
		if(Esp_buff == 'E')//the esp has got the message,start waiting
		{
			Esp_Flag = 1;//stop transmit 'A'
		}
		if(Esp_buff == 'B')
		{
//			if(Arm_Flag == 0)//get the medince
//			{
//				HAL_UART_Transmit(&huart2,&PV11,1,0xffff);
//				HAL_Delay(1000);
//			}
//			
//			if(Arm_buff == PV21)//successfully get the medince
//			{
//				Arm_Flag = 3;
//				Esp_Flag = 2;
//			}
			
			if(Arm_Flag == 3)//give the medince
			{
				HAL_UART_Transmit(&huart2,&PV12,1,0xffff);
				HAL_Delay(1000);
			}
			
			if(Arm_buff == PV31)//successfully give the medince
			{
				Arm_Flag = 3;
				Esp_Flag = 2;
			}
		
			//the door has already opened
		}
		if(Esp_Flag == 2)
		{
			HAL_UART_Transmit(&huart1,(uint8_t *)"C",1,0xffff);
			HAL_Delay(1000);
		}
		if(Esp_buff == 'F')
		{
			Esp_Flag = 0;//stop transmit 'C'
		}
		if(Esp_buff == 'D')
		{
			a2++;//the door has already closed
		}
		USART_1_IT();
		USART_2_IT();
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_TIM4_Init();
  MX_TIM9_Init();
  MX_TIM8_Init();
  MX_TIM6_Init();
  MX_TIM1_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_UART4_Init();
  MX_TIM7_Init();
  MX_UART5_Init();
  /* USER CODE BEGIN 2 */

	/*打开PWM通道*/
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
	
	/*打开定时器通道*/
	HAL_TIM_Base_Start(&htim4);
	HAL_TIM_Base_Start(&htim8);
  HAL_TIM_Base_Start_IT(&htim6);
	HAL_TIM_Base_Start_IT(&htim7);
	/*USART中断*/
	HAL_UART_Receive_IT(&huart1,&Esp_recvBuff,1);
	HAL_UART_Receive_IT(&huart2,&Arm_recvBuff,1);
	HAL_UART_Receive_IT(&huart5,&RC_CODE,1);
	HAL_UART_Receive_IT(&huart4,&Pi_buffer,1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
//		if(Arm_Flag == 0)//get the medince
//		{
//			HAL_UART_Transmit(&huart2,&PV11,1,0xffff);
//			HAL_Delay(1000);
//			}
//			
//			if(Arm_buff == PV21)//successfully get the medince
//			{
//				Arm_Flag = 3;
//				Esp_Flag = 2;
//			}
//			
//			if(Arm_Flag == 4)//give the medince
//			{
//				HAL_UART_Transmit(&huart2,&PV21,1,0xffff);
//				HAL_Delay(1000);
//			}
//			
//			if(Arm_buff == PV31)//successfully give the medince
//			{
//				Arm_Flag = 3;
//				Esp_Flag = 2;
//			}
//			USART_2_IT();
//      final_pid_set(80,80);
			RC_CMD();
	}	
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}
int fgetc(FILE *f)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, HAL_MAX_DELAY);
  return ch;
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
		
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   